package com.aisino.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aisino.annotation.RequireAuth;
import com.aisino.annotation.UserLoginToken;
import com.aisino.entity.User;
import com.aisino.service.impl.TokenService;
import com.aisino.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author guochuantao
 * @version 1.0
 * @description 测试第三方接口
 * @since 2024/7/22 上午9:33
 */
@RestController
@RequestMapping("/api")
public class ApiController {
    @GetMapping("/test/{id}")
    private String test(@PathVariable("id") String id) {
        User user = new User();
        user.setId(Integer.valueOf(id));
        user.setTeam("team111");
        user.setScore(21.5);
        user.setPlayer("player111");
        user.setCreateTime(new Date());
        return JSONUtil.toJsonPrettyStr(user);
    }

    @RequireAuth //basic auth
    @GetMapping("/test2")
    private User test2(@RequestParam("id") String id) {
        User user = new User();
        user.setId(Integer.valueOf(id));
        user.setTeam("team111");
        user.setScore(21.5);
        user.setPlayer("player111");
        user.setCreateTime(new Date());
        return user;
    }
    @RequireAuth //basic auth
    @PostMapping("/test3")
    private String test3(@RequestBody JSONObject object) {
        User user = new User();
        user.setId(Integer.valueOf(object.get("id").toString()));
        user.setTeam("team111");
        user.setScore(21.5);
        user.setPlayer(object.get("player").toString());
        user.setCreateTime(new Date());
        return JSONUtil.toJsonPrettyStr(user);
    }

    @UserLoginToken //jwt
    @GetMapping("/test")
    private String test() {
        String tokenUserId = TokenUtils.getTokenUserId();
        System.out.println("tokenUserId = " + tokenUserId);

        User user = new User();
        user.setId(RandomUtil.randomInt());
        user.setTeam("team111");
        user.setScore(21.5);
        user.setPlayer("player111");
        user.setCreateTime(new Date());
        return JSONUtil.toJsonPrettyStr(user);
    }

    @UserLoginToken   //jwt
    @PostMapping("/test1")
    private String test1(@RequestBody JSONObject object) {
        User user = new User();
        user.setId(Integer.valueOf(object.get("id").toString()));
        user.setTeam("team111");
        user.setScore(21.5);
        user.setPlayer(object.get("player").toString());
        user.setCreateTime(new Date());
        return JSONUtil.toJsonPrettyStr(user);
    }

    @PostMapping("/test")
    private String test(@RequestBody JSONObject object) {
        User user = new User();
        user.setId(Integer.valueOf(object.get("id").toString()));
        user.setTeam("team111");
        user.setScore(21.5);
        user.setPlayer(object.get("player").toString());
        user.setCreateTime(new Date());
        return JSONUtil.toJsonPrettyStr(user);
    }

    @Resource
    private TokenService tokenService;

    @PostMapping("/login")
    private String login(@RequestBody JSONObject object, HttpServletResponse response) {
        String id = object.get("id").toString();
        //todo dao操作等
        String username = object.get("username").toString();
        String password = object.get("password").toString();
        if ("admin".equals(username) && "123456".equals(password)) {
            User user = new User();
            user.setId(1314520);
            user.setPassword("123456");
            String token = tokenService.getToken(user);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("token", token);

            //设置cookie
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);

            return JSONUtil.toJsonPrettyStr(jsonObject);
        }else {
            return "账户名或者密码错误";
        }
    }


}
